#include <bits/stdc++.h>
using namespace std;
int ttt = 0;
bool ishuiwenshu(int m)
{
    int n = m;
    int x = 0;
    while (n > 0)
    {
        x = x * 10 + n % 10;
        n = n / 10;
    }
    if (x == m)
    {
        return true;
    }
    return false;
}
int huiwenshu(int m)
{
    ttt += 1;
    if (ttt > 8)
    {
        ttt = 0;
        return 0;
    }
    if (ishuiwenshu(m))
    {
        return 1;
    }
    else
    {
        int n = m;
        int x = 0;
        while (n > 0)
        {
            x = x * 10 + n % 10;
            n = n / 10;
        }
        return 1 + huiwenshu(m + x);
    }
}

int huiwen(int m)
{
    int t = 0;
    while (!ishuiwenshu(m))
    {
        t += 1;
        int n = m;
        int x = 0;
        while (n > 0)
        {
            x = x * 10 + n % 10;
            n = n / 10;
        }
        m = m + x;
        if (t >= 8)
        {
            break;
        }
    }
    return t;
}
int main()
{
    int l;
    cin >> l;
    while (l--)
    {
        int m;
        cin >> m;
        int k = huiwen(m);
        if (k >= 8)
        {
            cout << 0 << endl;
        }
        else
        {
            cout << k << endl;
        }
    }
}